import instance from "../../utils/util"
const app = getApp();
// pages/shopinfo/shopinfo.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    show: false,
    num: 1,
    content: "",
    blessing: "",
    num:1,
    all: 0,
    skuId:""
  },
  add(){
      this.data.num++;
      this.setData({
          num:this.data.num
      })
  },
  jian(){
      if(this.data.num !== 1){
        this.data.num--;
        this.setData({
            num:this.data.num
        })
      }


  },
 async addgwc(){
   if(wx.getStorageSync('info')){
    let data=JSON.parse(wx.getStorageSync('info'));
    const res=await instance.post("/api/shopping-carts",{
        userId:data.id, 
        productId:this.data.info.id,
        //  skuId:this.data.skuId,
          quantity:this.data.num
    })
    console.log(res)
    wx.showToast({
      title: '成功',
    })
   }else{
    wx.showToast({
      title: '请登录',
      icon:'none'
    })
     
   }
   
  },
  fanhui(){
      wx.navigateBack()
  },
  onShow() {
    if (app.token !== '') {
      this.getNum()

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.id)
    this.getinfo(options.id)

  },
  async getinfo(e) {
    const res = await instance.get(`/api/products/${e}`);
    console.log(res)
    this.setData({
      info: res.data
    })
    const ress=await instance.get(`/api/product-skus/${e}/skus`);
    console.log(ress)
    // this.setData({
    //     skuId:ress.data[0].id
    // })

  },
  async getNum() {
    const res = await instance.get("/cart/getCartList");
    console.log(res)
    let num = 0;
    res.data.forEach(el => {
      num += el.count
    })
    this.setData({
      all: num
    })
  },
  onClickIcon() {},
  cart() {
    this.setData({
      show: true,
      content: "加入购物车"
    })
  },
  cart1() {
    this.setData({
      show: true,
      content: "立即购买"
    })

  },
  onClose() {
    this.setData({
      show: false
    })

  },
  onChange(e) {
    console.log(e)
    this.setData({
      num: e.detail
    })

  },
  async save() {
    console.log(app.token)
    if (app.token == '') {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    } else if (this.data.content == '加入购物车') {
      const res = await instance.get(`/cart/addToCart/${this.data.info.id}/${this.data.num}?blessing=${this.data.blessing}`)
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '保存成功',
        })
        this.setData({
          show: false
        })
        this.getNum()
      }

    }else if(this.data.content == '立即购买'){
        // const res = await instance.get(`/order/buy/${this.data.info.id}`);
        // console.log(res)
        wx.navigateTo({
          url: `/pages/jsdd/jsdd?id=${this.data.info.id}`,
        })
        
    }
  },
  onClickButton() {},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})